package cate.game.activity.config;

import cate.common.table.activity.MergeDayActivityBaseRow;
import cate.common.table.activity.NaturalActivityBaseRow;
import cate.common.table.activity.OpenDayActivityBaseRow;
import cate.common.table.d.GDActivity;
import cate.common.util.TimeTool;
import cate.game.framework.ModuleMan;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

@NoteClass("活动基础配置")
public class ActivityCfgHolder extends ModuleMan {
    @NoteField(value = "全局活动开启配置",detail = "包括当前和未来会开启的开服天数以及自然日活动")
    private List<ActivityBaseCfg> globalConfigs = new ArrayList<>();

    @Override
    public void initDependent() throws Exception {
        loadExcel();
    }

    public void onReload(){
        loadExcel();
    }

    private void loadExcel() {
        game.table.activity.openDay.getList().forEach(row -> createCycle(game.getOpenTime(), row));
        game.table.activity.natural.getList().forEach(row -> createNatural(game.getOpenTime(), row));

        if (game.getMergeTime() > 0) {
            game.table.activity.merge.getList().forEach(row -> createMerge(game.getMergeTime(), row));
        }
        log.info("全服活动数据加载完毕,共有{}个活动", globalConfigs.size());
    }

    /**
     * 开服天数活动
     * @param openTime
     * @param row
     */
    private void createCycle(long openTime, OpenDayActivityBaseRow row) {
        ActivityBaseCfg cfg = new ActivityBaseCfg();
        cfg.init(row);
        cfg.startTime = TimeTool.calTimeStamp(openTime, row.startDay, row.startTimeStr);
        cfg.endTime = TimeTool.calTimeStamp(openTime, row.endDay, row.endTimeStr);
        if (cfg.endTime < game.time.now()) {
            return;
        }
        //检测是否可以添加这个活动
        if(allowAddCycle(openTime, cfg)) {
            globalConfigs.add(cfg);
        }
    }

    /**
     * 自然日活动
     * @param openTime
     * @param row
     */
    private void createNatural(long openTime, NaturalActivityBaseRow row) {
        ActivityBaseCfg cfg = new ActivityBaseCfg();
        cfg.init(row);
        cfg.startTime = TimeTool.parseTime(row.startTimeStr);
        cfg.endTime = TimeTool.parseTime(row.endTimeStr);
        if (cfg.endTime < game.time.now()) {
            return;
        }
        //有开服天数限制的活动
        if (row.openDay > 0) {
            if (row.openDayType == GDActivity.OpenDayType.FIRST_DAY) {
                //活动开始时是开服第几天
                int openDay = TimeTool.getDistanceDays(openTime, cfg.startTime) + 1;
                if (openDay < row.openDay) {
                    return;
                }
            }
            if (row.openDayType == GDActivity.OpenDayType.MIDWAY) {
                //根据开服天数限制推测的活动开启时间
                long openDayStartTime = TimeTool.calTimeStamp(openTime, row.openDay);

                if (openDayStartTime >= cfg.endTime) {
                    return;
                }
                if (openDayStartTime > cfg.startTime) {
                    cfg.startTime = openDayStartTime;
                }
            }
        }
        globalConfigs.add(cfg);
    }


    /**
     * 合服天数活动
     * @param mergeTime
     * @param row
     */
    private void createMerge(long mergeTime, MergeDayActivityBaseRow row) {
        ActivityBaseCfg cfg = new ActivityBaseCfg();
        cfg.init(row);
        cfg.startTime = TimeTool.calTimeStamp(mergeTime, row.startDay, row.startTimeStr);
        cfg.endTime = TimeTool.calTimeStamp(mergeTime, row.endDay, row.endTimeStr);
        if (cfg.endTime < game.time.now()) {
            return;
        }
        globalConfigs.add(cfg);
    }


    public List<ActivityBaseCfg> getAllCfg(){
        return globalConfigs;
    }


    private boolean allowAddCycle(long openTime, ActivityBaseCfg cfg) {
        switch (cfg.funcId) {
            default:
                return true;
        }
    }
}
